package gateway.controller;

import common.controller.base.BaseController;
import common.model.response.base.Result;
import common.service.RedissonCacheService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 系统缓存管理
 *
 * @author 米泽鹏
 * @since 2022-02-26
 */
@Api(tags = "系统缓存管理")
@RestController
@RequestMapping("/gateway/cache")
public class CacheController extends BaseController {

	@Resource
	private RedissonCacheService redissonCacheService;

	@ApiOperation(value = "更新所有缓存", notes = "更新所有缓存", response = Result.class)
	@PostMapping("/base/updateAllCache")
	public Result<?> updateAllCache() {
		redissonCacheService.cacheSysUser().throwExceptionIfFailed();
		redissonCacheService.cacheSysUserRole().throwExceptionIfFailed();
		redissonCacheService.cacheSysRoleAuthority().throwExceptionIfFailed();
		redissonCacheService.cacheSysAuthority().throwExceptionIfFailed();
		redissonCacheService.cacheSysBusinessCode().throwExceptionIfFailed();
		return success();
	}

	@ApiOperation(value = "更新用户缓存", notes = "更新用户缓存", response = Result.class)
	@PostMapping("/base/cacheSysUser")
	public Result<?> cacheSysUser() {
		redissonCacheService.cacheSysUser().throwExceptionIfFailed();
		return success();
	}

	@ApiOperation(value = "更新用户-角色缓存", notes = "更新用户-角色缓存", response = Result.class)
	@PostMapping("/base/cacheSysUserRole")
	public Result<?> cacheSysUserRole() {
		redissonCacheService.cacheSysUserRole().throwExceptionIfFailed();
		return success();
	}

	@ApiOperation(value = "更新角色-权限缓存", notes = "更新角色-权限缓存", response = Result.class)
	@PostMapping("/base/cacheSysRoleAuthority")
	public Result<?> cacheSysRoleAuthority() {
		redissonCacheService.cacheSysRoleAuthority().throwExceptionIfFailed();
		return success();
	}

	@ApiOperation(value = "更新权限缓存", notes = "更新权限缓存", response = Result.class)
	@PostMapping("/base/cacheSysAuthority")
	public Result<?> cacheSysAuthority() {
		redissonCacheService.cacheSysAuthority().throwExceptionIfFailed();
		return success();
	}

	@ApiOperation(value = "更新业务编码缓存", notes = "更新业务编码缓存", response = Result.class)
	@PostMapping("/base/cacheSysBusinessCode")
	public Result<?> cacheSysBusinessCode() {
		redissonCacheService.cacheSysBusinessCode().throwExceptionIfFailed();
		return success();
	}

}
